package com.sofwin.configer;

import com.google.code.kaptcha.BackgroundProducer;
import com.google.code.kaptcha.GimpyEngine;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.text.WordRenderer;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * @packageName: com.sofwin.configer
 * @author: andyliu
 * @date: 2022/5/9 14:29
 * @email 115176513@qq.com
 * @description: TODO
 */
public class MyProducer extends DefaultKaptcha {
    @Override
    public String createText() {
        // 1+1=?
        char[] ysfs={'+','*',' ',' '};
        Random random = new Random();
        // 第一个数字
        int num1 = random.nextInt(10);
        // 第二个数字
        int num2 = random.nextInt(10);
        int num3 = num3 =random.nextInt(2);
        if(num1>num2){
            ysfs[2]='-';
            num3=random.nextInt(3);
            if(num2!=0 && num1%num2==0){
                ysfs[3]='/';
                num3 =random.nextInt(4);
            }
        }

        // 运算符
        char ysf = ysfs[num3];// 当前的运算符
        StringBuffer str = new StringBuffer();
        str.append(num1);
        str.append(ysf);
        str.append(num2);
        str.append("=?");
        int result=0;
        if(ysf=='+'){
            result=num1+num2;
        }
        if(ysf=='-'){
            result=num1-num2;
        }
        if(ysf=='*'){
            result=num1*num2;
        }
        if(ysf=='/'){
            result=num1/num2;
        }
        str.append(result);

        return str.toString();
    }


}
